package com.gopay.bis.common.map2j;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ObjectMapSwtich.java
 * @author liujy
 * @2012-4-21 下午8:39:04 www.gopay.com.cn Inc.All rights reserved.
 */
public class ObjectMapSwtich {

    /**
     * 
     * @param obj
     * @return
     * @throws Exception
     */
    public static Map<String, Object> introspect(Object obj) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        BeanInfo info = Introspector.getBeanInfo(obj.getClass());
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            Method reader = pd.getReadMethod();
            if (reader != null && !"class".equals(pd.getName())) {
                result.put(pd.getName(), reader.invoke(obj));
            }
        }
        return result;
    }

    /**
     * 
     * @param o
     * @return
     */
    public static Map<String, Object> getMap(Object o) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            result.put(field.getName(), field.get(o));
        }
        return result;
    }

}
